---
title: "Democracies in Peril (Trends)"
author: "Hashem Krayem"
date: "2025-21-02"
output:
  pdf_document:
    keep_tex: yes
    extra_dependencies: rotating, float
    fig_caption: yes
  word_document: default
  html_document:
    df_print: paged
fontsize: 12pt
---

```{r, warning=FALSE, message=FALSE, echo = F}
knitr::opts_chunk$set(echo = F)
knitr::opts_chunk$set(fig.height=8, fig.width = 10, dpi = 144)
knitr::opts_chunk$set(cache = TRUE)
#install.packages("") for any that are not available
library(tidyverse)
library(haven)
library(kableExtra)
library(knitr)
library(readxl)
library(stringr)
library(interplot)
library(margins)
library(plm)
library(pcse)
library(stargazer)
library(lmtest)
```

```{r}
# Place the Rmd file in the same folder as the data set 
combined_data <- read_dta("Bastiaens Rudra Replication stata data.dta")
```

```{r}
# OECD variable 

# List of 24 OECD members in 1990
oecd_1990 <- c("Australia", "Austria", "Belgium", "Canada", "Denmark", 
               "Finland", "France", "Germany", "Greece", "Iceland", 
               "Ireland", "Italy", "Japan", "Luxembourg", "Netherlands", 
               "New Zealand", "Norway", "Portugal", "Spain", "Sweden", 
               "Switzerland", "United Kingdom", "United States") # keep Turkiye

```

```{r}
# Manually define list of standing regimes (1990-2012)

dem <- c("Argentina", "Bolivia", "Botswana", "Brazil", "Bulgaria", "Chile", 
         "Colombia", "Costa Rica", "Cyprus", "Czechia", "El Salvador", 
         "Estonia", "Honduras", "Hungary", "India", "Jamaica", 
         "Korea, Rep.", "Latvia", "Lithuania", "North Macedonia", 
         "Mauritius", "Namibia", "Nicaragua", "Panama", "Philippines", 
         "Slovak Republic", 
         "Slovenia", "Trinidad and Tobago", "Turkiye", "Ukraine", "Uruguay")

non_dem <- c("Algeria", "Azerbaijan", "Bahrain", "Bhutan", "Burkina Faso", 
             "Cambodia", "Cameroon", "Central African Republic", 
             "China", "Congo, Rep.", 
             "Cote d'Ivoire", "Egypt, Arab Rep.", "Ethiopia", "Guinea", 
             "Iran, Islamic Rep.", "Jordan", 
             "Kazakhstan", "Kuwait", "Morocco", "Mozambique", "Oman", 
             "Papua New Guinea", "Qatar", "Rwanda", "Singapore", "Suriname", 
             "Syrian Arab Republic", "Tajikistan", "Tanzania", "Togo", 
             "Tunisia", 
             "Uganda", "Yemen, Rep.", "Zimbabwe")

# length(dem): 31
# length(non_dem): 34
sample <- c(dem, non_dem)
```

```{r, message=F, warning=FALSE}
# Creating a regime type variable
combined_data$regime <- ifelse(is.na(combined_data$e_polity2) == T, NA, 
                           ifelse(combined_data$e_polity2 > 5, 1, 0))

combined_data$regimetype <- ifelse(combined_data$regime == 1, "Democracy", 
                               ifelse(combined_data$regime == 0, "Non-democracy", 
                                      NA))
```

# Figure 1: Yearly Averages for constant regimes (1990−2018) in WDI 

```{r, message=F, warning=FALSE}
combined_data_cr <- combined_data %>% 
  filter(year >= 1990 & year <= 2018) %>% 
  filter(wbcountryname %in% sample)

# % Revenue
# Subset data for the sample of longstanding regimes
yearly_averages <- combined_data_cr %>%
  filter(!is.na(regimetype)) %>% 
  group_by(year, regimetype) %>%
  summarise(
    Av_TradePercRevenue = mean(as.numeric(taxesonintltradeofrevenue), 
                  na.rm = TRUE), 
    Av_DomTaxRev = mean(as.numeric(domtaxrev_sum_gdp)*100/as.numeric(revenueexclgrantsofgdp), na.rm = TRUE))

WDI_cr_1990_2018 <- ggplot(yearly_averages) +
  geom_line(aes(x = as.numeric(year), y = Av_TradePercRevenue, 
            color = "Trade Taxes (% Revenue)")) +
  geom_line(aes(x = as.numeric(year), y = Av_DomTaxRev, 
            color = "Domestic Taxes (sum of income and goods/services) (% Revenue)")) +
  scale_color_manual(
    name = "Average values",  
    values = c("Trade Taxes (% Revenue)" = "blue", 
               "Domestic Taxes (sum of income and goods/services) (% Revenue)" = "red")) +
  labs(title = "Yearly Averages for the sample of stable regimes (1990-2018)",
       x = "Year",
       y = "Values", 
       caption = "WDI Data") +
  theme_minimal() +
  facet_wrap(~ factor(regimetype), 
             ncol = 2)

WDI_cr_1990_2018

ggsave("WDI_cr_1990_2018.png", plot = WDI_cr_1990_2018, width = 8, height = 6, units = "in")

```

# Appendix, Figure A.1: Tax Revenue Trends in a Sample of Stable, Non-Resource Rich Democracies and Non-Democracies, 1990-2018 (WDI Data)

```{r}
# Calculate the upper quartile of fuel_exp
upper_quartile <- quantile(as.numeric(combined_data$fuelexportsofmerchandiseexportst), 0.9, na.rm = TRUE)

# Filter countries in the top 25% of fuel exports as a percentage of merchandise exports
resource_rich_results <- combined_data %>% filter(!wbcountryname %in% oecd_1990) %>% 
  filter(as.numeric(fuelexportsofmerchandiseexportst) >= upper_quartile) %>%
  dplyr::select(wbcountryname, wbcountrycode, fuelexportsofmerchandiseexportst)

rr_countries <- combined_data %>%
  filter(as.numeric(fuelexportsofmerchandiseexportst) >= upper_quartile) %>% filter(!wbcountryname %in% oecd_1990) %>% 
  dplyr::select(wbcountryname) %>% unlist() %>% as.vector() %>% unique()
```

```{r, message=F, warning=FALSE}
combined_data_cr_rr <- combined_data %>% 
  filter(year >= 1990 & year <= 2018) %>% 
  filter(wbcountryname %in% sample) %>% 
  filter(!wbcountryname %in% rr_countries)

```

```{r, message=F, warning=FALSE}
# % Revenue
# Subset data for the sample of longstanding regimes
yearly_averages <- combined_data_cr_rr %>%
  filter(!is.na(regimetype)) %>% 
  group_by(year, regimetype) %>%
  summarise(
    Av_TradePercRevenue = mean(as.numeric(taxesonintltradeofrevenue), 
                  na.rm = TRUE), 
    Av_DomTaxRev = mean(as.numeric(domtaxrev_sum_gdp)*100/as.numeric(revenueexclgrantsofgdp), na.rm = TRUE))

WDI_cr_1990_2018_ex_rr <- ggplot(yearly_averages) +
  geom_line(aes(x = as.numeric(year), y = Av_TradePercRevenue, 
            color = "Trade Taxes (% Revenue)")) +
  geom_line(aes(x = as.numeric(year), y = Av_DomTaxRev, 
            color = "Domestic Taxes (sum of income and goods/services) (% Revenue)")) +
  scale_color_manual(
    name = "Average values",  
    values = c("Trade Taxes (% Revenue)" = "blue", 
               "Domestic Taxes (sum of income and goods/services) (% Revenue)" = "red")) +
  labs(title = "Yearly Averages for the sample of stable regimes (1990-2018)",
       x = "Year",
       y = "Values", 
       caption = "WDI Data, excluding resource-rich") +
  theme_minimal() +
  facet_wrap(~ factor(regimetype), 
             ncol = 2)
WDI_cr_1990_2018_ex_rr

ggsave("WDI_cr_1990_2018_ex_rr.png", plot = WDI_cr_1990_2018_ex_rr, width = 8, height = 6, units = "in")

```

# Appendix, Figure A.2: Tax Revenue Trends in a Sample of Stable Democracies and Non-Democracies, 1990-2018 (ICTD Data)

```{r, message=F, warning=FALSE}
combined_data_cr <- combined_data %>% 
  filter(year >= 1990 & year <= 2018) %>% 
  filter(wbcountryname %in% sample)

```

```{r, message=F, warning=FALSE}
# % Revenue
# Subset data for the sample of longstanding regimes
yearly_averages <- combined_data_cr %>%
  filter(!is.na(regimetype)) %>% 
  group_by(year, regimetype) %>%
  summarise(
    Av_TradePercRevenue = mean(as.numeric(tax_trade)*100/as.numeric(rev_ex_gr_ex_sc), 
                  na.rm = TRUE), 
    Av_DomTaxRev = mean(as.numeric(domtaxrev_sum_ictd)*100/as.numeric(rev_ex_gr_ex_sc), na.rm = TRUE))

ICTD_cr_1990_2018 <- ggplot(yearly_averages) +
  geom_line(aes(x = as.numeric(year), y = Av_TradePercRevenue, 
            color = "Trade Taxes (% Revenue)")) +
  geom_line(aes(x = as.numeric(year), y = Av_DomTaxRev, 
            color = "Domestic Taxes (sum of income and goods/services) (% Revenue)")) +
  scale_color_manual(
    name = "Average values",  
    values = c("Trade Taxes (% Revenue)" = "blue", 
               "Domestic Taxes (sum of income and goods/services) (% Revenue)" = "red")) +
  labs(title = "Yearly Averages for the sample of stable regimes (1990-2018)",
       x = "Year",
       y = "Values", 
       caption = "ICTD Data") +
  theme_minimal() +
  facet_wrap(~ factor(regimetype), 
             ncol = 2)

ICTD_cr_1990_2018

ggsave("ICTD_cr_1990_2018.png", plot = ICTD_cr_1990_2018, width = 8, height = 6, units = "in")

```

# Appendix, Figure A.3: Tax Revenue Trends in a Sample of Stable, Non-Resource Rich Democracies and Non-Democracies, 1990-2018 (ICTD Data) 

```{r, message=F, warning=FALSE}
# % Revenue
# Subset data for the sample of longstanding regimes
yearly_averages <- combined_data_cr_rr %>%
  filter(!is.na(regimetype)) %>% 
  group_by(year, regimetype) %>%
  summarise(
    Av_TradePercRevenue = mean(as.numeric(tax_trade)*100/as.numeric(rev_ex_gr_ex_sc), 
                  na.rm = TRUE), 
    Av_DomTaxRev = mean(as.numeric(domtaxrev_sum_ictd)*100/as.numeric(rev_ex_gr_ex_sc), na.rm = TRUE))

ICTD_cr_1990_2018_ex_rr <- ggplot(yearly_averages) +
  geom_line(aes(x = as.numeric(year), y = Av_TradePercRevenue, 
            color = "Trade Taxes (% Revenue)")) +
  geom_line(aes(x = as.numeric(year), y = Av_DomTaxRev, 
            color = "Domestic Taxes (sum of income and goods/services) (% Revenue)")) +
  scale_color_manual(
    name = "Average values",  
    values = c("Trade Taxes (% Revenue)" = "blue", 
               "Domestic Taxes (sum of income and goods/services) (% Revenue)" = "red")) +
  labs(title = "Yearly Averages for the sample of stable regimes (1990-2018)",
       x = "Year",
       y = "Values", 
       caption = "ICTD Data, excluding resource-rich") +
  theme_minimal() +
  facet_wrap(~ factor(regimetype), 
             ncol = 2)

ICTD_cr_1990_2018_ex_rr

ggsave("ICTD_cr_1990_2018_ex_rr.png", plot = ICTD_cr_1990_2018_ex_rr, width = 8, height = 6, units = "in")

```
